<template>
  <div>
    <!-- 返利红票查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhRebateRedInvoiceApply"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props"> </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, uploadFile, saveData, batchUpdateStauts } from '@/api/omFinance/RebateRedInvoice/RebateRedInvoiceApply';
import { getDealerUser } from '@/api/omFinance';
import SonModel from './SonModel';
import SearchDealer from './ownerSeacher';
import { isArcfox } from '@/api/cms-api/origin/commonDealerType';

export default {
  name: 'zjRebateRedInvoiceQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isCan: false,
      visible: false,
      isDealer: false,
      visibleDialog: false,
      handleType: '',
      drawerTitle: '',
      ids: [],
      dealerCode: ''
    };
  },
  components: {
    SonModel,
    SearchDealer
  },
  created() {
    this.getUser();
    if (isArcfox()) {
      this.columns.find(x => x.dataIndex == 'salesOrgCode').editItems = [{ text: '北京蓝谷极狐投资管理有限公司销售组织', value: '1012' }];
      this.columns.find(x => x.dataIndex == 'salesOrgCode').filterItems = [{ text: '北京蓝谷极狐投资管理有限公司销售组织', value: '1012' }];
    } else {
      this.columns.find(x => x.dataIndex == 'salesOrgCode').editItems = [
        { text: '北京汽车销售有限公司', value: '1080' },
        { text: '株洲北汽汽车销售有限公司', value: '1180' },
        { text: '北京新能源汽车营销有限公司', value: '2000' },
        { text: '青岛卫蓝销售公司', value: '2001' }
      ];
      this.columns.find(x => x.dataIndex == 'salesOrgCode').filterItems = [
        { text: '北京汽车销售有限公司', value: '1080' },
        { text: '株洲北汽汽车销售有限公司', value: '1180' },
        { text: '北京新能源汽车营销有限公司', value: '2000' },
        { text: '青岛卫蓝销售公司', value: '2001' }
      ];
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    changeHandle(params) {
      params.dealerCodes = this.dealerCode;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      this.dealerCode = '';
      params.dealerCodes = '';
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    selectChangeHandle(row) { },
    dealerHandle() {
      console.log('---经销商---');
      this.isDealer = true;
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.userName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'createdBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'createdBy').filterItems = empList;
    },
    // 新增
    addHandle() { },
    // 编辑
    async editHandle(row, type) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    // 明细
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      let dealerCodes = [];
      if (data) {
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    async submitHandle(type) {
      let param = {
        operationType: type == 1 ? 1 : 2,
        rebateRedApplyIds: this.ids
      };
      const res = await batchUpdateStauts(param);
      if (res.resultCode == 200) {
        this.fetchParams = { ...this.fetchParams };
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.dealerHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6021')
        },
        {
          type: 'INPUT',
          label: '申请人',
          fieldName: 'applyPersonName',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'beginApplyDate|endApplyDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 60,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: props => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6021'),
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '6021')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          width: 200,
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '公司代码',
          dataIndex: 'salesOrgCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editItems: []
        },
        {
          title: '返利类型',
          dataIndex: 'rebateType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6036'),
          render: p => {
            return <span>{this.createDictText(p.row.rebateType, '6036')}</span>;
          }
        },
        {
          title: '总申请金额',
          dataIndex: 'totalApplyAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '申请日期',
          dataIndex: 'applyDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '申请人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '税率1(%)',
          dataIndex: 'taxRate1',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额1（含税）',
          dataIndex: 'includeTaxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额1',
          dataIndex: 'taxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额1',
          dataIndex: 'outcluedeTaxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },

        {
          title: '税率2(%)',
          dataIndex: 'taxRate2',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额2（含税）',
          dataIndex: 'includeTaxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额2',
          dataIndex: 'taxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额2',
          dataIndex: 'outcluedeTaxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '返利类型_1',
          dataIndex: 'rebateTypeD',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6036'),
          render: p => {
            return <span>{this.createDictText(p.row.rebateTypeD, '6036')}</span>;
          }
        },
        {
          title: '总申请金额_1',
          dataIndex: 'totalApplyAmountD',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税率1_1(%)',
          dataIndex: 'taxRate1D',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额1_1（含税）',
          dataIndex: 'includeTaxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额1_1',
          dataIndex: 'taxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额1_1',
          dataIndex: 'outcluedeTaxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },

        {
          title: '税率2_1(%)',
          dataIndex: 'taxRate2D',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额2_1（含税）',
          dataIndex: 'includeTaxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额2_1',
          dataIndex: 'taxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额2_1',
          dataIndex: 'outcluedeTaxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '红字发票信息表编号',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'range-dat'
        },

        {
          title: '审批意见',
          dataIndex: 'checkDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
